﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlanningGIS.Product
{ 
    /// <summary>
    /// 是否允许为空
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    public class AccecptNullAttribute : System.Attribute
    {
        private bool acceptNull;

        public AccecptNullAttribute(bool pAcceptNull)
        {
            this.acceptNull = pAcceptNull;
        }

        public bool AcceptNull
        {
            get { return this.acceptNull; }
        }
    }

    /// <summary>
    /// 是否必填
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    public class MustHaveValueAttribute : System.Attribute
    {
        private bool isMustHaveValue;

        public MustHaveValueAttribute(bool pIsMustHaveValue=true)
        {
            this.isMustHaveValue = pIsMustHaveValue;
        }

        public bool IsMustHaveValue
        {
            get { return this.isMustHaveValue; }
        }
    }

    /// <summary>
    /// 值从数组中选择
    /// 用于枚举类型的属性项，例如性别、数据更新单位等
    /// isSelectValue为false表示属性为非枚举型
    /// isSelectValue为true，accecptAddValue为true表示值为枚举型，并可新增除枚举值外的值,selectValues指向枚举类型
    /// isSelectValue为true，accecptAddValue为false表示值为枚举型，并只能从中选择值,selectValues指向枚举类型
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    public class IsSelectValueAttribute : System.Attribute
    {
        /// <summary>
        /// 是否值从数组中选择
        /// </summary>
        private bool isSelectValue;

        /// <summary>
        /// 是否可新增值
        /// </summary>
        private bool accecptAddValue;

        /// <summary>
        /// 保存可能值的数组
        /// </summary>
        //private string[] selectValues;
        private System.Type selectValues;

        public IsSelectValueAttribute(bool pIsSelectValue)
        {
            init();
            this.isSelectValue = pIsSelectValue;
        }

        public IsSelectValueAttribute(bool pIsSelectValue, Type pSelectValues, bool accecptAddValue = false)
        {
            init();
            this.isSelectValue = pIsSelectValue;
            this.selectValues = pSelectValues;
            this.accecptAddValue = accecptAddValue;
        }

        private void init()
        {
            accecptAddValue = false;
        }

        public bool IsSelectValue
        {
            get { return this.isSelectValue; }
        }

        public bool AccecptAddValue
        {
            get { return this.accecptAddValue; }
        }

        public Type SelectValues
        {
            get { return this.selectValues; }
        }
    }

    /// <summary>
    /// 默认值
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    public class DefaultValueAttribute : System.Attribute
    {
        /// <summary>
        /// 是否有默认值
        /// </summary>
        private bool hasDefaultValue;

        /// <summary>
        /// 默认值
        /// 所有类型的字段默认值均作为字符串类型
        /// 实际使用时根据实际字段类型进行转换
        /// </summary>
        private string defaultValue;

        public DefaultValueAttribute(bool pHasDefaultValue = false, string pDefaultValue="")
        {
            this.hasDefaultValue = pHasDefaultValue;
            this.defaultValue = pDefaultValue;
        }

        public DefaultValueAttribute(string pDefaultValue)
        {
            this.hasDefaultValue = true;
            this.defaultValue = pDefaultValue;
        }

        /// <summary>
        /// 设置特殊默认值
        /// </summary>
        /// <param name="pHasDefaultValue"></param>
        /// <param name="pSpecialDefaultValue">1 表示当前日期；2表示当前时间；3表示当前日期和时间</param>
        public DefaultValueAttribute(bool pHasDefaultValue,int pSpecialDefaultValue)
        {
            this.hasDefaultValue = true;

            switch (pSpecialDefaultValue)
            {
                case 1:
                    // 当前日期
                    this.defaultValue = DateTime.Now.ToShortDateString();
                    break;
                case 2:
                    // 当前时间
                    this.defaultValue = DateTime.Now.ToShortTimeString();
                    break;
                case 3:
                    // 当前日期和时间
                    this.defaultValue = DateTime.Now.ToLongTimeString();
                    break;
            }            
        }

        public bool HasDefaultValue
        {
            get { return this.hasDefaultValue; }
        }

        public string DefaultValue
        {
            get { return this.defaultValue; }
        }
    }

    /// <summary>
    /// 长度
    /// </summary>
    [AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
    public class LengthAttribute : System.Attribute
    {
        public int Length
        {
            get;
            set;
        }

        public LengthAttribute()
        {
            Length = -1;
        }

        public LengthAttribute(int pLength)
        {
            Length = pLength;
        }
    }
}